//
//  SubDistrictDetailController.m
//  YunGui
//
//  Created by Hanen 3G 01 on 15/11/13.
//  Copyright © 2015年 hanen. All rights reserved.
//

#import "SubDistrictDetailController.h"
#import "UIView+Extension.h"
#import "SubDistrictDetailModel.h"

#define labelHeight  [UIView getHeight:20.0f]

@interface SubDistrictDetailController ()
{
    NSArray       *_titleArray;
    
    UIScrollView  *_scrollView;
    
    UIView  *_mainView;
    CGFloat   space;
    SubDistrictDetailModel *detailModel;
    NSDictionary *dataDict ;
    
}


@end

@implementation SubDistrictDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [ViewTool setNavigationBar:self.navigationController.navigationBar];
    self.navigationItem.leftBarButtonItem=[ViewTool getBackBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    space = [UIView getHeight:10.0f];
    self.navigationItem.title = @"小区详情";
    _titleArray = @[@"小区名称",@"所属地区",@"地址",@"小区类型",@"物业公司",@"当前户数",@"交付时间",@"负责人",@"创建人",@"等级",@"竞争对手"];
    
    [self creatUI];
    [self initDetailData];
}
//自定义的leftBarButton
- (UIBarButtonItem *)changeBarButtonWithImageName:(NSString *)imageName
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    return barItem;
}
- (void)creatUI
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    _scrollView.backgroundColor = GrayBackColor;
    [self.view addSubview:_scrollView];
    
    _mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 2 * space, kScreenWidth, [UIView getHeight:280])];
    _mainView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_mainView];
    
    UIImageView  *iamgeV = [[UIImageView alloc] initWithFrame:CGRectMake(2.5  *space, 1.5 *space, 20.0f, 20.0f)];
//    iamgeV.image = [UIImage imageNamed:<#(nonnull NSString *)#>]
//    iamgeV.backgroundColor = BlueColor;
    iamgeV.image = [UIImage imageNamed:@"Btn_Normal_Xinxi"];
    [_mainView addSubview:iamgeV];
    
    UILabel *titlelabel= [ViewTool getLabelWith:CGRectMake(iamgeV.maxX +  1.5 *space, iamgeV.y, 120, 20.0f) WithTitle:@"小区详情" WithFontSize:16.0f WithTitleColor:UIColorFromRGB(0x636267) WithTextAlignment:NSTextAlignmentLeft];
    [_mainView addSubview:titlelabel];
    
    UIView *line1 =[ViewTool getLineViewWith:CGRectMake(titlelabel.x, titlelabel.maxY + 1 * space, kScreenWidth - titlelabel.x, 1) withBackgroudColor:GrayLineColor];
    [_mainView addSubview:line1];
    
    for (int i = 0; i < _titleArray.count;i ++) {
//        CGFloat width = [_titleArray[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, labelHeight) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0f], NSForegroundColorAttributeName : [UIColor blackColor]} context:nil].size.width;
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(line1.x, line1.maxY +  space +  labelHeight * i, 70.0f, labelHeight)];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [ViewTool getFont:15.0f];
        label.text = _titleArray[i];
        label.textColor = GRAYCOLOR2;
        [_mainView addSubview:label];
        
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(label.maxX + space, label.y, kScreenWidth - label.maxX - 2 *space, labelHeight)];
        contentLabel.tag = 333 + i;
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.text = @"";
        contentLabel.textColor = BLACKTITLECOLOR;
        contentLabel.font = [ViewTool getFont:15.0f];
//        contentLabel.backgroundColor = [UIColor brownColor];
         [_mainView addSubview:contentLabel];
        
        
    }
    
    
    _scrollView.contentSize = CGSizeMake(0, _mainView.maxY + space);
    UIView *linetop =[ViewTool getLineViewWith:CGRectMake(0, 0, kScreenWidth, 1) withBackgroudColor:GrayLineColor];
    [_mainView addSubview:linetop];
    
    UIView *linebottom =[ViewTool getLineViewWith:CGRectMake(0, _mainView.height - 1, kScreenWidth, 1) withBackgroudColor:GrayLineColor];
    [_mainView addSubview:linebottom];
    
}
//给contentLabel赋值 利用tag


- (void)initDetailData
{
//    NSNumber *idNUm = [NSNumber numberWithUnsignedLong:[self.districtID longLongValue]];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{@"method":@"app.village.detail",@"id" : self.districtID}];
    NSDictionary *paramesDict = [DataTool addSignToDic:dict];
    NSLog(@"%@*-*-*-*-***-*-*",paramesDict);
    [DataTool postWithUrl:HEAD_URL parameters:paramesDict success:^(id data) {
        id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
        if ([backData[@"status"] isEqualToString:@"SUCCESS"]) {
            NSLog(@"小区详情的数据:%@",backData);
            dataDict = backData[@"data"];
//            [detailModel setValuesForKeysWithDictionary:backData[@"data"]];
            
            [self useData];
        }else{
            [Tools showMessage:backData[@"msg"] view:self];
        }
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

- (void)useData
{
    for (int i = 0; i<_titleArray.count; i++) {
        UILabel *label = [_mainView viewWithTag:333 + i];
        if (i == 0) {
            NSLog(@"%@",detailModel.title);
//            label.text = detailModel.title;
              label.text = dataDict[@"title"];
        }else if (i == 1){
            NSString *palce;
            if ([dataDict[@"province"] isEqualToString:dataDict[@"city"]] || [dataDict[@"province"] isEqualToString:@"0"]) {
                palce = [NSString stringWithFormat:@"%@",dataDict[@"city"]];
            }else{
                palce = [NSString stringWithFormat:@"%@ %@",dataDict[@"province"],dataDict[@"city"]];
            }
            label.text = palce;
        }else if (i == 2){
//            label.text = detailModel.address;
             label.text = dataDict[@"address"];
        }else if (i == 3){
//            label.text = detailModel.type;
              label.text = dataDict[@"type"];
        }else if (i == 4){
//            label.text = detailModel.property;
            label.text = dataDict[@"property"];
        }else if (i == 5){
//            label.text = detailModel.current;
            label.text = dataDict[@"current"];
        }else if (i == 6){
//            label.text = detailModel.updated_at;
            label.text = dataDict[@"updated_at"];
        }else if (i == 7){
//            label.text = detailModel.m_user_name;
            label.text = dataDict[@"m_user_name"];
        }else if (i == 8){
            //            label.text = detailModel.m_user_name;
            label.text = dataDict[@"user_name"];
        }else if (i == 9){
//            label.text = detailModel.level_id;
            label.text = dataDict[@"level"];
        }else if (i == 10){
//            label.text = detailModel.competitor;
            label.text = dataDict[@"competitor_names"];
        }
    }
    
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
